package com.artisan.view;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.ImageIcon;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.swing.JComboBox;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import com.artisan.dao.CourseDao;
import com.artisan.dao.SelectedCourseDao;
import com.artisan.dao.StudentDao;
import com.artisan.model.Course;
import com.artisan.model.SelectedCourse;
import com.artisan.model.Student;

public class ManangeSelectedCourseFrm extends JInternalFrame {
	private JTable selectedCourseListTable;
	private JComboBox searchCourseComboBox;
	private JComboBox searchStudentComboBox;
	private JComboBox editSelectedStudentComboBox;
	private JComboBox editSelectedCourseComboBox;
	private List<Student> studentList = new ArrayList<Student>();
	private List<Course> courseList = new ArrayList<Course>();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ManangeSelectedCourseFrm frame = new ManangeSelectedCourseFrm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ManangeSelectedCourseFrm() {
		setClosable(true);
		setIconifiable(true);

		setTitle("选课管理");
		setFrameIcon(new ImageIcon(ManangeSelectedCourseFrm.class.getResource("/com/artisan/image/选择.png")));
		setBounds(100, 100, 893, 612);

		JLabel lblNewLabel = new JLabel("学生:");
		lblNewLabel.setIcon(new ImageIcon(ManangeSelectedCourseFrm.class.getResource("/com/artisan/image/学生管理.png")));
		lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		searchStudentComboBox = new JComboBox();
		searchStudentComboBox.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent ie) {
				studentChangeAct(ie);
			}
		});

		JLabel lblNewLabel_1 = new JLabel("课程:");
		lblNewLabel_1.setIcon(new ImageIcon(ManangeSelectedCourseFrm.class.getResource("/com/artisan/image/课程.png")));
		lblNewLabel_1.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		searchCourseComboBox = new JComboBox();

		JButton confirmSelectedButton = new JButton("确认选课");
		confirmSelectedButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				confirmSelectedCourse(ae);
			}
		});
		confirmSelectedButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		confirmSelectedButton
				.setIcon(new ImageIcon(ManangeSelectedCourseFrm.class.getResource("/com/artisan/image/确认.png")));

		JScrollPane scrollPane = new JScrollPane();

		JPanel panel = new JPanel();
		panel.setBorder(
				new TitledBorder(null, "\u4FEE\u6539\u9009\u8BFE", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup().addGap(78).addGroup(groupLayout
						.createParallelGroup(Alignment.LEADING).addComponent(scrollPane, Alignment.TRAILING)
						.addComponent(panel, GroupLayout.DEFAULT_SIZE, 716, Short.MAX_VALUE)
						.addGroup(groupLayout.createSequentialGroup()
								.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 61, GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(searchStudentComboBox, GroupLayout.PREFERRED_SIZE, 173,
										GroupLayout.PREFERRED_SIZE)
								.addGap(56)
								.addComponent(lblNewLabel_1, GroupLayout.PREFERRED_SIZE, 58, GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(ComponentPlacement.UNRELATED)
								.addComponent(searchCourseComboBox, GroupLayout.PREFERRED_SIZE, 191,
										GroupLayout.PREFERRED_SIZE)
								.addGap(52).addComponent(confirmSelectedButton)))
						.addGap(87)));
		groupLayout.setVerticalGroup(groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup().addGap(39)
						.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(lblNewLabel)
								.addComponent(searchStudentComboBox, GroupLayout.PREFERRED_SIZE,
										GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(searchCourseComboBox, GroupLayout.PREFERRED_SIZE,
										GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblNewLabel_1).addComponent(confirmSelectedButton))
						.addGap(18)
						.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 321, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.UNRELATED)
						.addComponent(panel, GroupLayout.PREFERRED_SIZE, 149, GroupLayout.PREFERRED_SIZE)
						.addContainerGap(19, Short.MAX_VALUE)));

		selectedCourseListTable = new JTable();
		selectedCourseListTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent me) {
				selectedCourse(me);
			}
		});
		selectedCourseListTable.setModel(new DefaultTableModel(new Object[][] {},
				new String[] { "\u9009\u8BFE\u7F16\u53F7", "\u5B66\u751F\u59D3\u540D", "\u8BFE\u7A0B\u540D\u79F0" }) {
			boolean[] columnEditables = new boolean[] { false, false, false };

			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		scrollPane.setViewportView(selectedCourseListTable);

		JLabel lblNewLabel_2 = new JLabel("学生:");
		lblNewLabel_2.setIcon(new ImageIcon(ManangeSelectedCourseFrm.class.getResource("/com/artisan/image/学生管理.png")));
		lblNewLabel_2.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		editSelectedStudentComboBox = new JComboBox();

		JLabel lblNewLabel_3 = new JLabel("课程:");
		lblNewLabel_3.setIcon(new ImageIcon(ManangeSelectedCourseFrm.class.getResource("/com/artisan/image/课程.png")));
		lblNewLabel_3.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		editSelectedCourseComboBox = new JComboBox();

		JButton confirmEditButton = new JButton("确认修改");
		confirmEditButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				editSelectCourseAct(ae);
			}
		});
		confirmEditButton
				.setIcon(new ImageIcon(ManangeSelectedCourseFrm.class.getResource("/com/artisan/image/确认.png")));
		confirmEditButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));

		JButton deleteCourseButton = new JButton("退选课程");
		deleteCourseButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				deleteSelectedCourse(ae);
			}
		});
		deleteCourseButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		deleteCourseButton
				.setIcon(new ImageIcon(ManangeSelectedCourseFrm.class.getResource("/com/artisan/image/删除.png")));
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup().addGroup(gl_panel.createParallelGroup(Alignment.TRAILING)
						.addGroup(gl_panel.createSequentialGroup().addContainerGap().addComponent(confirmEditButton,
								GroupLayout.PREFERRED_SIZE, 117, GroupLayout.PREFERRED_SIZE))
						.addGroup(Alignment.LEADING, gl_panel.createSequentialGroup().addGap(18)
								.addComponent(lblNewLabel_2, GroupLayout.PREFERRED_SIZE, 58, GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(ComponentPlacement.UNRELATED).addComponent(editSelectedStudentComboBox,
										GroupLayout.PREFERRED_SIZE, 175, GroupLayout.PREFERRED_SIZE)))
						.addGap(113)
						.addComponent(lblNewLabel_3, GroupLayout.PREFERRED_SIZE, 58, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
								.addComponent(editSelectedCourseComboBox, GroupLayout.PREFERRED_SIZE, 171,
										GroupLayout.PREFERRED_SIZE)
								.addComponent(deleteCourseButton, GroupLayout.PREFERRED_SIZE, 119,
										GroupLayout.PREFERRED_SIZE))
						.addContainerGap(97, Short.MAX_VALUE)));
		gl_panel.setVerticalGroup(gl_panel.createParallelGroup(Alignment.LEADING).addGroup(gl_panel
				.createSequentialGroup().addContainerGap()
				.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE).addComponent(lblNewLabel_2)
						.addComponent(editSelectedStudentComboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
								GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNewLabel_3).addComponent(editSelectedCourseComboBox,
								GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
				.addGap(39).addGroup(gl_panel.createParallelGroup(Alignment.BASELINE).addComponent(confirmEditButton)
						.addComponent(deleteCourseButton))
				.addContainerGap(27, Short.MAX_VALUE)));
		panel.setLayout(gl_panel);
		getContentPane().setLayout(groupLayout);
		setStudentCombox();
		setCourseCombox();
		initTable();
		setAuthority();
	}

	protected void deleteSelectedCourse(ActionEvent ae) {
		// TODO 自动生成的方法存根
		int row = selectedCourseListTable.getSelectedRow();
		if (row == -1) {
			JOptionPane.showMessageDialog(this, "请选择要修改的数据！");
			return;
		}
		int selected_id = Integer.parseInt(selectedCourseListTable.getValueAt(row, 0).toString());
		String courseName = selectedCourseListTable.getValueAt(row, 2).toString();
		SelectedCourse sc = new SelectedCourse();
		sc.setId(selected_id);
		SelectedCourseDao scDao = new SelectedCourseDao();
		CourseDao courseDao = new CourseDao();
		if (scDao.delete(selected_id)) {
			if (courseDao.updateSelectedNum(getCourseIdByName(courseName), -1)) {
				JOptionPane.showMessageDialog(this, "退课成功！");
			} else {
				JOptionPane.showMessageDialog(this, "退课成功，更新课程信息失败！");
			}
		} else {
			JOptionPane.showMessageDialog(this, "退课失败！");
		}
		scDao.closeDao();
		courseDao.closeDao();
		initTable();
	}

	protected void editSelectCourseAct(ActionEvent ae) {
		// TODO 自动生成的方法存根
		int row = selectedCourseListTable.getSelectedRow();
		if (row == -1) {
			JOptionPane.showMessageDialog(this, "请选择要修改的数据！");
			return;
		}
		int selected_id = Integer.parseInt(selectedCourseListTable.getValueAt(row, 0).toString());
		String studentName = selectedCourseListTable.getValueAt(row, 1).toString();
		String courseName = selectedCourseListTable.getValueAt(row, 2).toString();
		Student student = (Student) editSelectedStudentComboBox.getSelectedItem();
		Course course = (Course) editSelectedCourseComboBox.getSelectedItem();
		if (studentName.equals(student.getName()) && courseName.equals(course.getName())) {
			JOptionPane.showMessageDialog(this, "还未修改数据！");
			return;
		}
		CourseDao courseDao = new CourseDao();
		if (!courseDao.selectedEnable(course.getId())) {
			JOptionPane.showMessageDialog(this, "该课程已经选满，不能再选!");
			return;
		}

		SelectedCourse sc = new SelectedCourse();
		sc.setId(selected_id);
		sc.setStudent_id(student.getId());
		sc.setCourse_id(course.getId());
		SelectedCourseDao scDao = new SelectedCourseDao();
		if (scDao.isSelected(sc)) {
			JOptionPane.showMessageDialog(this, "已经选过这门课程了，不能再选!");
			return;
		}
		if (scDao.updateSelectedCourse(sc)) {
			if (courseDao.updateSelectedNum(sc.getCourse_id(), 1)) {
				if (courseDao.updateSelectedNum(getCourseIdByName(courseName), -1)) {
					JOptionPane.showMessageDialog(this, "修改成功！!");
				}
			} else {
				JOptionPane.showMessageDialog(this, "修改成功，课程信息更新失败!");
			}
		}
		courseDao.closeDao();
		scDao.closeDao();
		initTable();
	}

	protected void selectedCourse(MouseEvent me) {
		// TODO 自动生成的方法存根
		int row = selectedCourseListTable.getSelectedRow();
		String studentName = selectedCourseListTable.getValueAt(row, 1).toString();
		String courseName = selectedCourseListTable.getValueAt(row, 2).toString();
		for (int i = 0; i < editSelectedStudentComboBox.getItemCount(); i++) {
			Student student = (Student) editSelectedStudentComboBox.getItemAt(i);
			if (studentName.equals(student.getName())) {
				editSelectedStudentComboBox.setSelectedIndex(i);
				break;
			}
		}
		for (int i = 0; i < editSelectedCourseComboBox.getItemCount(); i++) {
			Course course = (Course) editSelectedCourseComboBox.getItemAt(i);
			if (courseName.equals(course.getName())) {
				editSelectedCourseComboBox.setSelectedIndex(i);
				break;
			}
		}
	}

	protected void studentChangeAct(ItemEvent ie) {
		// TODO 自动生成的方法存根
		if (ie.getStateChange() == ItemEvent.SELECTED) {
			initTable();
		}
	}

	protected void confirmSelectedCourse(ActionEvent ae) {
		// TODO 自动生成的方法存根
		Student student = (Student) searchStudentComboBox.getSelectedItem();
		Course scourse = (Course) searchCourseComboBox.getSelectedItem();
		SelectedCourse sc = new SelectedCourse();
		sc.setStudent_id(student.getId());
		sc.setCourse_id(scourse.getId());
		CourseDao courseDao = new CourseDao();
		if (!courseDao.selectedEnable(scourse.getId())) {
			JOptionPane.showMessageDialog(this, "该课程已经选满，不能再选!");
			return;
		}
		SelectedCourseDao scDao = new SelectedCourseDao();
		if (scDao.isSelected(sc)) {
			JOptionPane.showMessageDialog(this, "已经选过这门课程了，不能再选!");
			return;
		}
		if (scDao.addSelectedCourse(sc)) {
			if (courseDao.updateSelectedNum(sc.getCourse_id(), 1)) {
				JOptionPane.showMessageDialog(this, "选课成功！!");
			} else {
				JOptionPane.showMessageDialog(this, "选课成功，课程信息更新失败!");
			}
		} else {
			JOptionPane.showMessageDialog(this, "选课失败!");
		}
		courseDao.closeDao();
		scDao.closeDao();
		initTable();
	}

	private void setStudentCombox() {
		StudentDao studentDao = new StudentDao();
		studentList = studentDao.getStudentList(new Student());
		studentDao.closeDao();
		for (Student student : studentList) {
			searchStudentComboBox.addItem(student);
			editSelectedStudentComboBox.addItem(student);
		}
		if ("学生".equals(MainFrm.userType.getName())) {
			Student user = (Student) MainFrm.userObject;
			for (int i = 0; i < searchStudentComboBox.getItemCount(); i++) {
				Student student = (Student) searchStudentComboBox.getItemAt(i);
				if (student.getId() == user.getId()) {
					searchStudentComboBox.setSelectedIndex(i);
					editSelectedStudentComboBox.setSelectedIndex(i);
					break;
				}
			}
		}
	}

	private void setCourseCombox() {
		CourseDao courseDao = new CourseDao();
		courseList = courseDao.getCourseList(new Course());
		courseDao.closeDao();
		for (Course course : courseList) {
			searchCourseComboBox.addItem(course);
			editSelectedCourseComboBox.addItem(course);
		}
	}

	private void getSelectedCourse(SelectedCourse selectedCourse) {
		SelectedCourseDao selectedCourseDao = new SelectedCourseDao();
		List<SelectedCourse> selectedCourseList = selectedCourseDao.getSelectedCourseList(selectedCourse);
		DefaultTableModel dft = (DefaultTableModel) selectedCourseListTable.getModel();
		dft.setRowCount(0);
		for (SelectedCourse sc : selectedCourseList) {
			Vector v = new Vector();
			v.add(sc.getId());
			v.add(getStudentNameById(sc.getStudent_id()));
			v.add(getCourseNameById(sc.getCourse_id()));
			dft.addRow(v);
		}
		selectedCourseDao.closeDao();
	}

	private void initTable() {
		Student student = (Student) searchStudentComboBox.getSelectedItem();
		SelectedCourse sc = new SelectedCourse();
		sc.setStudent_id(student.getId());
		getSelectedCourse(sc);
	}

	private String getStudentNameById(int id) {
		for (int i = 0; i < studentList.size(); i++) {
			if (studentList.get(i).getId() == id) {
				return studentList.get(i).getName();
			}
		}
		return "";
	}

	private String getCourseNameById(int id) {
		for (int i = 0; i < courseList.size(); i++) {
			if (id == courseList.get(i).getId()) {
				return courseList.get(i).getName();
			}
		}
		return "";
	}

	private int getCourseIdByName(String name) {
		for (int i = 0; i < courseList.size(); i++) {
			if (name.equals(courseList.get(i).getName())) {
				return courseList.get(i).getId();
			}
		}
		return 0;
	}

	private void setAuthority() {
		if ("学生".equals(MainFrm.userType.getName())) {
			searchStudentComboBox.setEnabled(false);
			editSelectedStudentComboBox.setEnabled(false);
		}
	}
}
